package com.edu.test;

import com.edu.bean.Book;
import com.edu.bean.Writer;
import com.edu.mapper.WriterMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Mytest {

    private SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";

        InputStream inputStream = Resources.getResourceAsStream(path);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void test001(){
        WriterMapper writerMapper = sqlSession.getMapper(WriterMapper.class);
        List<Writer> writerList= writerMapper.selectAllWriter();
        for (Writer writer : writerList) {
            System.out.println(writer);
        }
    }

    @Test
    public void test002(){
        WriterMapper writerMapper = sqlSession.getMapper(WriterMapper.class);
        List<Writer> writerList= writerMapper.selectAllWriterAndBook();
        for (Writer writer : writerList) {
            System.out.println(writer);
            for (Book book : writer.getBookList()) {
                System.out.println(book);
            }
        }
    }
    @Test
    public void test003(){
        WriterMapper writerMapper = sqlSession.getMapper(WriterMapper.class);
        List<Writer> writerList= writerMapper.selectAllWriterAndBookByName("杨");
        for (Writer writer : writerList) {
            System.out.println(writer);
            for (Book book : writer.getBookList()) {
                System.out.println(book);
            }
        }
    }
}
